﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Web;

using jade.core;
using jade.core.behaviours;
using jade.lang.acl;
using jade.wrapper.gateway;
using System.Net;
using System.Data.SqlClient;
using Newtonsoft.Json;

namespace CMS1 {
    public class DummyReceiver : Agent {
        SqlConnection cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\macmsdb.mdf;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        SqlDataReader dr;

        public override void setup() {
            cmd.Connection = cn;
            // Enable O2A communication
            //setEnabledO2ACommunication(true, 10);
            addBehaviour(new DummyReceiveBehaviour());
            Console.WriteLine("DummyReceiver " + getName() + " created!");
        }

        private void insert() {
            cn.Open();
            cmd.CommandText = "insert into table";
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        private void read() {
            cn.Open();
            cmd.CommandText = "select * from table";
            dr = cmd.ExecuteReader();
            if (dr.HasRows) {
                LinkedList<string> elements = new LinkedList<string>();
                while (dr.Read()) {
                    elements.AddLast(dr[0].ToString());
                }
            }
            cn.Close();
        }
        private void delete() {
            cn.Open();
            cmd.CommandText = "delete from table";
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        private void update() {
            cn.Open();
            cmd.CommandText = "update table set";
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public override void takeDown() {
            Console.WriteLine("ReceiverDummyAgent " + getLocalName() + " deleting...");
            //setEnabledO2ACommunication(false, 0);
        }
    }

    public class DummyReceiveBehaviour : CyclicBehaviour {
        public override void action() {
            //MessageTemplate messageTemplate = MessageTemplate.MatchPerformative(ACLMessage.INFORM);
            ACLMessage msg = myAgent.receive();//messageTemplate);
            if (msg == null) {
                block();
            } else {
                Message parsedMsg = JsonConvert.DeserializeObject<Message>(msg.getContent());
                switch (parsedMsg.Purpose) {
                    case 1:
                        ACLMessage reply = msg.createReply();
                        reply.setContent(msg.getSender().getLocalName());
                        myAgent.send(reply);
                        Console.WriteLine("New message received from " + msg.getSender().getName());
                        Console.WriteLine("Message content: " + msg.getContent());
                        break;
                    case 2:
                        //throw new Exception(msg.getContent());
                        HttpContext.Current.Response.Redirect("http://www.google.com/");
                        //TextBox1.Text = msg.getContent();
                        break;
                }
            }
        }
    }
}